package com.qiguliuxing.dts.db.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qiguliuxing.dts.db.dao.DtsRegionMapper;
import com.qiguliuxing.dts.db.domain.DtsRegion;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DtsRegionService {

	@Resource
	private DtsRegionMapper regionMapper;

	public List<DtsRegion> getAll() {
		QueryWrapper<DtsRegion> queryWrapper = new QueryWrapper<>();
		byte b = 4;
		queryWrapper.lambda().ne(DtsRegion::getType, b);
		return regionMapper.selectList(queryWrapper);
	}

	public List<DtsRegion> queryByPid(Integer parentId) {
		QueryWrapper<DtsRegion> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(DtsRegion::getPid, parentId);
		return regionMapper.selectList(queryWrapper);
	}

	public DtsRegion findById(Integer id) {
		return regionMapper.selectById(id);
	}

	public List<DtsRegion> querySelective(String name, Integer code, Integer page, Integer size, String sort,
			String order) {
		QueryWrapper<DtsRegion> queryWrapper = new QueryWrapper<>();
		if (StrUtil.isNotEmpty(name)) {
			queryWrapper.lambda().like(DtsRegion::getName, name);
		}
		if (code != null) {
			queryWrapper.lambda().eq(DtsRegion::getCode, code);
		}
		if (StrUtil.isNotEmpty(sort) && StrUtil.isNotEmpty(order)) {
			queryWrapper.orderByDesc(sort);
		}
		IPage<DtsRegion> iPage = new Page<>(page, size);

		return regionMapper.selectPage(iPage, queryWrapper).getRecords();
	}
}
